package com.xingkeduo.pojo.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.xingkeduo.pojo.query.PermsQuery;
import com.xingkeduo.pojo.table.PermsTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

public class PermsJson extends BaseJson {

	public static PermsQuery createQueryForPerms(String json) throws Throwable {
		PermsQuery query = new PermsQuery();
		JsonNode root = validJson(json);
		if (root == null)
			return query;
		query.setId(validId(root, YesNoEnum.YES));
		return query;
	}

	public static PermsTable createTableForModifyPerms(String urlParam, PermsTable permsTable) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");
		permsTable.setId(validId(root, YesNoEnum.YES));

		return permsTable;
	}


	public static ObjectNode toJsonOne(PermsTable permsTable) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();

		if (permsTable != null)
		{
			root.put("id", permsTable.getId());
			root.put("pid", permsTable.getPid());
			root.put("name", permsTable.getName());
			root.put("boardCode", permsTable.getBoardCode());
			root.put("type", permsTable.getType());
			root.put("value", permsTable.getValue());
			root.put("url", permsTable.getUrl());
			root.put("sort", permsTable.getSort());
			root.put("status", permsTable.getStatus());
			root.put("font", permsTable.getFont());
			root.put("icon", permsTable.getIcon());

			if(!CommonUtil.isEmpty(permsTable.getChildren())){
				root.put("haschild",1);
			}else{
				root.put("haschild",0);
			}

			//处理时间
			String createdTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(permsTable.getCreatedTime());
			root.put("createtime", createdTime);

			String updateTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(permsTable.getUpdatedTime());
			root.put("updatetime", updateTime);
		}

		return root;
	}

    public static ArrayNode toJson(List<PermsTable> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();
        if (!CommonUtil.isEmpty(tables))
			for (PermsTable permsTables : tables) {
				root.add(toJsonOne(permsTables));
				if(permsTables.getChildren().size() > 0){
					for (PermsTable permsTable1 :permsTables.getChildren()) {
						root.add(toJsonOne(permsTable1));
						if(permsTable1.getChildren().size() > 0){
							for (PermsTable permsTable2 :permsTable1.getChildren()) {
								root.add(toJsonOne(permsTable2));
								if(permsTable1.getChildren().size() > 0){
									for (PermsTable permsTable3 :permsTable2.getChildren()) {
										root.add(toJsonOne(permsTable3));
									}
								}
							}
						}

					}
				}
			}
            /*for (Iterator<PermsTable> iterator = tables.iterator(); iterator.hasNext();) {
                root.add(toJson(iterator.next()));
                if(iterator.next().getChildren().size() > 0){
                	toJson(iterator.next().getChildren());
				}
            }*/
        return root;
    }


	public static ArrayNode listToJson(List<PermsTable> tables) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if (!CommonUtil.isEmpty(tables))
			for (Iterator<PermsTable> iterator = tables.iterator(); iterator.hasNext();) {
				root.add(toJson(iterator.next()));
			}
		return root;
	}

	public static ObjectNode toJson(PermsTable permsTable) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();

		if (permsTable != null)
		{
			root.put("id", permsTable.getId());
			root.put("pid", permsTable.getPid());
			root.put("name", permsTable.getName());
			root.put("type", permsTable.getType());
			root.put("value", permsTable.getValue());
			root.put("url", permsTable.getUrl());
			root.put("sort", permsTable.getSort());
			root.put("font", permsTable.getFont());
			root.put("icon", permsTable.getIcon());
			root.put("checked", permsTable.getChecked());

		}

		return root;
	}

	public static ObjectNode toJsonPage(Page<PermsTable> page) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();
		if (page != null)
		{
			root.put("pageNo", page.getPageNo());
			root.put("pageSize", page.getPageSize());
			root.put("pageTotal", page.getPageTotal());
			root.put("recordTotal", page.getRecordTotal());
			root.put("dtoLists", toJson(page.getDtoList()));
		}
		return root;
	}

}
